/****************************************************************************
 *
 * Copyright (C) 2003-2010 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.juniper;

import java.sql.Timestamp;
import java.util.StringTokenizer;

import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecordField;
import nfse.gui.server.GUIRecordFactory;
import nfse.gui.server.RecordBuilderUtil;

public class NetScreen5400RecordFactory extends GUIRecordFactory<NetScreen5400Record> {

    public NetScreen5400RecordFactory() {
        builder = new RecordBuilderUtil();
        builder.addColumn(null, "ID", RecordBuilderUtil.BIGINT);
        RecordBuilderUtil.SQLField field = builder.addColumn("Start TS", "startts", RecordBuilderUtil.TIMESTAMP);
        field = builder.addColumn("Duration", "duration", RecordBuilderUtil.INTEGER);
        field = builder.addColumn("Src IP", "srcip", RecordBuilderUtil.INET);
        field = builder.addColumn("Src Port", "srcport", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PORT);
        field = builder.addColumn("Src Bytes", "srcbytes", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Dst IP", "dstip", RecordBuilderUtil.INET);
        field = builder.addColumn("Dst Port", "dstport", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PORT);
        field = builder.addColumn("Dst Bytes", "dstbytes", RecordBuilderUtil.BIGINT);
        field = builder.addColumn("Protocol", "protocol", RecordBuilderUtil.INTEGER);
        field.setSubtype(FlexRecordField.SUB_TYPE_PROTOCOL);
        field = builder.addColumn("Service", "service", RecordBuilderUtil.VARCHAR, NetScreen5400Record.MAX_SERVICE_LEN);
        field = builder.addColumn("Action", "action", RecordBuilderUtil.VARCHAR, NetScreen5400Record.MAX_ACTION_LEN);
    }

    @Override
    protected void setValues(NetScreen5400Record record, int id) {
        int idx = -1;
        builder.setFieldValue(++idx, "" + id);
        builder.setFieldValue(++idx, record.getStartTS().toString());
        builder.setFieldValue(++idx, "" + record.duration);
        builder.setFieldValue(++idx, record.getSrcAddr());
        builder.setFieldValue(++idx, "" + record.srcPort);
        builder.setFieldValue(++idx, "" + record.srcBytes);
        builder.setFieldValue(++idx, "" + record.getDstAddr());
        builder.setFieldValue(++idx, "" + record.dstPort);
        builder.setFieldValue(++idx, "" + record.dstBytes);
        builder.setFieldValue(++idx, "" + record.proto);
        builder.setFieldValue(++idx, "" + record.service);
        builder.setFieldValue(++idx, "" + record.action);
    }

    @Override
    public NetFSERecord generate(String line, int sensor) throws Exception {

        NetScreen5400Record temp = new NetScreen5400Record();
        temp.setInitialString(line);
        temp.setTypeName("NetScreen");
        temp.setSensorID(sensor);

        int colIdx = line.indexOf(":", 15);
        if (colIdx == -1)
            return null;

        line = line.substring(colIdx + 1);
        int startIdx = line.indexOf("start_time");
        if (startIdx == -1)
            return null;
        else
            line = line.substring(startIdx);

        StringTokenizer st = new StringTokenizer(line);
        String startStr = st.nextToken();
        startStr = startStr.substring(startStr.indexOf("\"") + 1);
        startStr += " " + st.nextToken();
        startStr = startStr.substring(0, startStr.length() - 1);
        temp.setStartTS(Timestamp.valueOf(startStr));

        // Next is the duration (duration=X)
        String token = st.nextToken();
        temp.duration = Integer.parseInt(token.substring(token.indexOf("=") + 1));

        // Next is the policy_id (policy_id=X)
        token = st.nextToken();
        int policyID = Integer.parseInt(token.substring(token.indexOf("=") + 1));

        // Next is the service (service=), ignore it and use the next token
        // instead to determine proto
        token = st.nextToken();
        String service = token.substring(token.indexOf("=") + 1);
        while (!(token = st.nextToken()).startsWith("proto=")) {
            service += " " + token;
        }
        temp.service = service;

        // Next is the protocol (proto=X)
        String protoStr = token.substring(token.indexOf("=") + 1);
        temp.proto = Integer.parseInt(protoStr);

        // Now process src information
        if (!st.nextToken().equals("src"))
            return null;

        token = st.nextToken();
        String srcZone = token.substring(token.indexOf("=") + 1);

        // Dst information is next
        if (!st.nextToken().equals("dst"))
            return null;

        token = st.nextToken();
        String dstZone = token.substring(token.indexOf("=") + 1);

        token = st.nextToken();
        temp.action = token.substring(token.indexOf("=") + 1);

        while (!(token = st.nextToken()).startsWith("sent=")) {
            temp.action += " " + token;
        }

        if (!token.startsWith("sent="))
            return null;

        temp.srcBytes = Long.parseLong(token.substring(token.indexOf("=") + 1));
        token = st.nextToken();
        temp.dstBytes = Long.parseLong(token.substring(token.indexOf("=") + 1));
        token = st.nextToken();
        temp.setSrcAddr(token.substring(token.indexOf("=") + 1));
        token = st.nextToken();
        temp.setDstIP(NetFSEUtil.convertIPtoLong(token.substring(token.indexOf("=") + 1)));
        token = st.nextToken();
        if (token.equals("icmp")) {
            token = st.nextToken();
            temp.icmpType = Integer.parseInt(token.substring(token.indexOf("=") + 1));
        }

        boolean done = false;
        do {
            if (token.startsWith("session_id")) {
                temp.sessionId = token.substring(token.indexOf("=") + 1);
            } else if (token.startsWith("src_port")) {
                temp.srcPort = Integer.parseInt(token.substring(token.indexOf("=") + 1));
            } else if (token.startsWith("dst_port")) {
                temp.dstPort = Integer.parseInt(token.substring(token.indexOf("=") + 1));
            }
            if (st.hasMoreTokens())
                token = st.nextToken();
            else
                done = true;
        } while (!done);

        return temp;
    }

    @Override
    public NetFSERecord generate() {
        return new NetScreen5400Record();
    }

}
